package com.cloudservice.ccs.biz.impl;

import com.cloudservice.ccs.biz.IWorkOrderBiz;
import com.cloudservice.ccs.entity.WorkOrder;
import com.cloudservice.ccs.exception.WorkOrderException;
import com.cloudservice.ccs.mapper.WorkOrderMapper;
import com.cloudservice.ccs.model.WorkOrderConnectQuery;
import com.cloudservice.ccs.model.WorkOrderWithCustomerName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class WorkOrderImpl implements IWorkOrderBiz {
    @Autowired
    private WorkOrderMapper workOrderMapper;

    @Override
    public void save(WorkOrder workOrder) {
        int res = workOrderMapper.save(workOrder);
        if (res == 0) {
            throw new WorkOrderException("Create WorkOrder failed");
        }
    }

    @Override
    public void update(Integer id, WorkOrder workOrder) {
        int res = workOrderMapper.update(id, workOrder);
        if (res == 0) {
            throw new WorkOrderException("Update WorkOrder failed");
        }
    }

    @Override
    public void partialUpdate(Integer id, Map<String, Object> fields) {
        int res = workOrderMapper.partialUpdate(id, fields);
        if (res == 0) {
            throw new WorkOrderException("Partial update WorkOrder failed");
        }
    }

    @Override
    public void delete(Integer id) {
        int res = workOrderMapper.delete(id);
        if (res == 0) {
            throw new WorkOrderException("Delete WorkOrder failed");
        }
    }

    @Override
    public WorkOrder getOne(Integer id) {
        WorkOrder workOrder = workOrderMapper.getOne(id);
        if (workOrder == null) {
            throw new WorkOrderException("Requested WorkOrder does not exist");
        }
        return workOrder;
    }

    @Override
    public List<WorkOrderConnectQuery> getAll() {
        return workOrderMapper.getAll();
    }

    @Override
    public List<WorkOrderConnectQuery> getWithConditions(Map<String, Object> conditions) {
        return workOrderMapper.getWithConditions(conditions);
    }

    @Override
    public WorkOrderConnectQuery queryCustomer(Integer id) {
        WorkOrderConnectQuery workOrderConnectQuery = workOrderMapper.queryCustomer(id);
        if (workOrderConnectQuery == null) {
            throw new WorkOrderException("Get WorkOrder entity error: no such entity");
        }
        return workOrderConnectQuery;
    }

    @Override
    public List<WorkOrderWithCustomerName> queryByCustomerId(Integer customerId) {
        return workOrderMapper.queryByCustomerId(customerId);
    }
}
